using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using WP.Commons;

namespace MaxLeaf
{
    public partial class MakeWinForm : Form
    {
        ArrayList arryMtn = new ArrayList();
        ArrayList arryMtnDesign = new ArrayList();
        public MakeWinForm()
        {
            InitializeComponent();
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            if (this.txtTableName.Text.Trim() == "")
            {
                MessageBox.Show(Msg.Select("Need enter table name.", DataFunctions.GetMsg(this.Name, "MSG_NeedEnterTableName")));
                return;
            }
            if (this.txtNameSpace.Text.Trim() == "")
            {
                MessageBox.Show(Msg.Select("Need enter name space.", DataFunctions.GetMsg(this.Name, "MSG_NeedEnterNameSpace")));
                return;
            }

            string strUpdate = "";
            ////prjSysFunNet.clsProgToSql oSql = new clsProgToSql();
            string cond = "";
            ////strUpdate = WP.Commons.BaseFunctions.GetFieldsStringFromSQL(oSql.GetProgToSQl(this.txtTableName.Text, TypeOfSql.gSqlUseUpdate, ref  cond));

            if (strUpdate == "")
            {
                strUpdate = "*";
            }

            DataRow dr = WP.Commons.DataFunctions.GetDataRow(this.txtTableName.Text, strUpdate, "");
            this.lblProcess.Text = "Collect Mtn file info... ";
            this.lblProcess.Refresh();
            MakeMtn();
            this.lblProcess.Text = "Collect Mtn design file info... ";
            this.lblProcess.Refresh();
            MakeMtnDesign(dr);
            this.lblProcess.Text = "Make Mtn file ... ";
            this.lblProcess.Refresh();
            FileInfo finfo = new FileInfo(@"c:\frm" + this.txtTableName.Text + "Mtn.cs");
            if (finfo.Exists)
            {
                finfo.Delete();
            }
            using (StreamWriter sw = finfo.CreateText())
            {
                this.progressBar1.Minimum = 0;
                this.progressBar1.Maximum = arryMtn.Count ;
                this.progressBar1.Value = 0;
                foreach (string sline in arryMtn)
                {
                    sw.WriteLine(sline);
                    this.progressBar1.Value ++;
                }
            }

            this.lblProcess.Text = "Make Mtn design file ... ";
            this.lblProcess.Refresh();
            FileInfo finfo2 = new FileInfo(@"c:\frm" + this.txtTableName.Text + "Mtn.Designer.cs");
            if (finfo2.Exists)
            {
                finfo2.Delete();
            }
            using (StreamWriter sw2 = finfo2.CreateText())
            {
                this.progressBar1.Minimum = 0;
                this.progressBar1.Maximum = arryMtnDesign.Count;
                this.progressBar1.Value = 0;
                foreach (string sline in arryMtnDesign)
                {
                    sw2.WriteLine(sline);
                    this.progressBar1.Value++;
                }
            }
            MsgBoxEx.Show("Finished.");
        }

        private void MakeMtn()
        {
            this.progressBar1.Minimum =0;
            this.progressBar1.Maximum = 21;
            arryMtn.Add("using System;");
            arryMtn.Add("using System.Collections.Generic;");
            arryMtn.Add("using System.ComponentModel;");
            arryMtn.Add("using System.Data;");
            arryMtn.Add("using System.Drawing;");
            arryMtn.Add("using System.Text;");
            arryMtn.Add("using System.Windows.Forms;");
            arryMtn.Add("using System.Collections;");
            arryMtn.Add("using WP.Modules;");
            arryMtn.Add("using WP.Commons;");
            arryMtn.Add("namespace " + this.txtNameSpace.Text + ".Form");
            arryMtn.Add("{");
            arryMtn.Add("public partial class frm"+this.txtTableName.Text +"Mtn : BaseMtn ");
            arryMtn.Add("{");
            arryMtn.Add(" public frm" + this.txtTableName.Text + "Mtn()");
            arryMtn.Add("{");
            arryMtn.Add("  InitializeComponent();");
            arryMtn.Add(" LoadAllControl(new ArrayList(this.Controls)); ");
            arryMtn.Add(" }");
            arryMtn.Add("}");
            arryMtn.Add("}");
            this.progressBar1.Value = 20;
        }

        private void MakeMtnDesign(DataRow dr)
        {
            this.progressBar1.Minimum = 0;
            this.progressBar1.Maximum = 6;
            arryMtnDesign.Add("namespace "+this.txtNameSpace.Text + "Form");
            arryMtnDesign.Add("{");
            arryMtnDesign.Add("partial class frm"+this.txtTableName.Text +"Mtn");
            arryMtnDesign.Add("{");
            arryMtnDesign.Add("/// <summary>");
            arryMtnDesign.Add("/// Required designer variable.");
            arryMtnDesign.Add("/// </summary>");
            arryMtnDesign.Add("private System.ComponentModel.IContainer components = null;");
            arryMtnDesign.Add("/// <summary>");
            arryMtnDesign.Add("/// Clean up any resources being used.");
            arryMtnDesign.Add("/// </summary>");
            arryMtnDesign.Add("/// <param name='disposing'>true if managed resources should be disposed; otherwise, false.</param>");
            arryMtnDesign.Add("protected override void Dispose(bool disposing)");
            arryMtnDesign.Add("{");
            arryMtnDesign.Add("if (disposing && (components != null))");
            arryMtnDesign.Add("{");
            arryMtnDesign.Add("  components.Dispose();");
            arryMtnDesign.Add(" }");
            arryMtnDesign.Add(" base.Dispose(disposing);");
            arryMtnDesign.Add("}");
            arryMtnDesign.Add("#region Windows Form Designer generated code");
            arryMtnDesign.Add("/// <summary>");
            arryMtnDesign.Add("/// Required method for Designer support - do not modify");
            arryMtnDesign.Add("/// the contents of this method with the code editor.");
            arryMtnDesign.Add("/// </summary>");
            arryMtnDesign.Add(" private void InitializeComponent()");
            arryMtnDesign.Add("{");
            this.progressBar1.Value = 1;
            ///
            arryMtnDesign.Add("((System.ComponentModel.ISupportInitialize)(this.errProvider)).BeginInit();");
            arryMtnDesign.Add("this.SuspendLayout();");

            WP.DB.DBAccess dbaccess = new WP.DB.DBAccess(false, false);   

            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                if (dr.Table.Columns[i].ColumnName.ToUpper() == "RECID" || dr.Table.Columns[i].ColumnName.ToUpper() == "RECSTAMP") continue;
                DataType sType = DataFunctions.GetFieldType(dbaccess, this.txtTableName.Text  , dr.Table.Columns[i].ColumnName);
                arryMtnDesign.Add("this.lbl"+dr.Table.Columns[i].ColumnName+" = new System.Windows.Forms.Label();");
                switch (sType)
                {
                    case DataType.Bool :
                        arryMtnDesign.Add("this.utr" + dr.Table.Columns[i].ColumnName + " = new WP.Controls.TextRadioBox();");
                        break;
                    case DataType.Byte :
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + " = new System.Windows.Forms.TextBox();");
                        break;
                    case DataType.DateTime :
                        arryMtnDesign.Add("this.udt" + dr.Table.Columns[i].ColumnName + " = new WP.Controls.DateTimeEx();");
                        break;
                    case DataType.Integer :
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + " = new System.Windows.Forms.TextBox();");
                        break;
                    case DataType.Numeric :
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + " = new System.Windows.Forms.TextBox();");
                        break;
                    case DataType.String :
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + " = new System.Windows.Forms.TextBox();");
                        break;
                    default:
                        break;
                }
            }

            this.progressBar1.Value = 2;
            int x=0, y=0;
            
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                if (dr.Table.Columns[i].ColumnName.ToUpper() == "RECID" || dr.Table.Columns[i].ColumnName.ToUpper() == "RECSTAMP") continue;

                x = i / 20*116;
                y = i % 20 * 21;

                DataType sType = DataFunctions.GetFieldType(dbaccess, this.txtTableName.Text, dr.Table.Columns[i].ColumnName);
                arryMtnDesign.Add("this.lbl" + dr.Table.Columns[i].ColumnName + ".AutoSize = true;");
                arryMtnDesign.Add("this.lbl" + dr.Table.Columns[i].ColumnName + ".Location = new System.Drawing.Point("+x.ToString()+" , "+y.ToString()+");");
                arryMtnDesign.Add("this.lbl" + dr.Table.Columns[i].ColumnName + ".Name = "+'"'+"lbl" + dr.Table.Columns[i].ColumnName +'"'+ ";");
                arryMtnDesign.Add("this.lbl" + dr.Table.Columns[i].ColumnName + ".Size = new System.Drawing.Size(27, 13);");
                arryMtnDesign.Add("this.lbl" + dr.Table.Columns[i].ColumnName + ".Text = " + '"' + dr.Table.Columns[i].ColumnName + ":"+'"'+";" );

                switch (sType)
                {
                    case DataType.Bool:

                        arryMtnDesign.Add("this.utr" + dr.Table.Columns[i].ColumnName + ".BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;");
                        arryMtnDesign.Add("this.utr" + dr.Table.Columns[i].ColumnName + ".Location = new System.Drawing.Point(" + (x+75).ToString() + " , " + y.ToString() + ");");
                        arryMtnDesign.Add("this.utr" + dr.Table.Columns[i].ColumnName + ".Name =" + '"' + "utr" + dr.Table.Columns[i].ColumnName + '"' + ";");
                        arryMtnDesign.Add("this.utr" + dr.Table.Columns[i].ColumnName + ".Size =  new System.Drawing.Size(41, 20);");
                        arryMtnDesign.Add("this.utr" + dr.Table.Columns[i].ColumnName + ".TabIndex = " + i.ToString() + " ;");
                        arryMtnDesign.Add("this.utr" + dr.Table.Columns[i].ColumnName + ".TextRadioBoxMode = WP.Controls.TextRadioBox.ControlMode.Common;");
                        break;
                    case DataType.Byte:
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".Location = new System.Drawing.Point(" + (x + 75).ToString() + " , " + y.ToString() + ");");
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".Name =" + '"' + "txt" + dr.Table.Columns[i].ColumnName + '"' + ";");
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".Size =  new System.Drawing.Size(41, 20);");
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".TabIndex = " + i.ToString() + " ;");
                        break;
                    case DataType.DateTime:

                        arryMtnDesign.Add("this.udt" + dr.Table.Columns[i].ColumnName + ".BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;");
                        arryMtnDesign.Add("this.udt" + dr.Table.Columns[i].ColumnName + ".MaxDate = new System.DateTime(9999, 12, 31, 0, 0, 0, 0);");
                        arryMtnDesign.Add("this.udt" + dr.Table.Columns[i].ColumnName + ".MinDate = new System.DateTime(1900, 1, 2, 0, 0, 0, 0);");
                        arryMtnDesign.Add("this.udt" + dr.Table.Columns[i].ColumnName + ".Value = " + '"' + "  /  /    " + '"' + ";");

                        arryMtnDesign.Add("this.udt" + dr.Table.Columns[i].ColumnName + ".Location = new System.Drawing.Point(" + (x + 75).ToString() + " , " + y.ToString() + ");");
                        arryMtnDesign.Add("this.udt" + dr.Table.Columns[i].ColumnName + ".Name =" + '"' + "udt" + dr.Table.Columns[i].ColumnName + '"' + ";");
                        arryMtnDesign.Add("this.udt" + dr.Table.Columns[i].ColumnName + ".Size =  new System.Drawing.Size(87, 20);");
                        arryMtnDesign.Add("this.udt" + dr.Table.Columns[i].ColumnName + ".TabIndex = " + i.ToString() + " ;");
                        break;
                    case DataType.Integer:

                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".Location = new System.Drawing.Point(" + (x + 75).ToString() + " , " + y.ToString() + ");");
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".Name =" + '"' + "txt" + dr.Table.Columns[i].ColumnName + '"' + ";");
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".Size =  new System.Drawing.Size(41, 20);");
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".TabIndex = " + i.ToString() + " ;");
                        break;
                    case DataType.Numeric:

                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".Location = new System.Drawing.Point(" + (x + 75).ToString() + " , " + y.ToString() + ");");
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".Name =" + '"' + "txt" + dr.Table.Columns[i].ColumnName + '"' + ";");
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".Size =  new System.Drawing.Size(41, 20);");
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".TabIndex = " + i.ToString() + " ;");
                        break;
                    case DataType.String:

                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".Location = new System.Drawing.Point(" + (x + 75).ToString() + " , " + y.ToString() + ");");
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".Name =" + '"' + "txt" + dr.Table.Columns[i].ColumnName + '"' + ";");
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".Size =  new System.Drawing.Size(41, 20);");
                        arryMtnDesign.Add("this.txt" + dr.Table.Columns[i].ColumnName + ".TabIndex = " + i.ToString() + " ;");
                        break;
                    default:
                        break;
                }
            }
            this.progressBar1.Value = 3;

            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                if (dr.Table.Columns[i].ColumnName.ToUpper() == "RECID" || dr.Table.Columns[i].ColumnName.ToUpper() == "RECSTAMP") continue;
                DataType sType = DataFunctions.GetFieldType(dbaccess, this.txtTableName.Text, dr.Table.Columns[i].ColumnName);
                arryMtnDesign.Add("this.Controls.Add(this.lbl" + dr.Table.Columns[i].ColumnName +");");

                switch (sType)
                {
                    case DataType.Bool:
                        arryMtnDesign.Add("this.Controls.Add(this.utr" + dr.Table.Columns[i].ColumnName + " );");
                        break;
                    case DataType.Byte:
                        arryMtnDesign.Add("this.Controls.Add(this.txt" + dr.Table.Columns[i].ColumnName + " );");
                        break;
                    case DataType.DateTime:
                        arryMtnDesign.Add("this.Controls.Add(this.udt" + dr.Table.Columns[i].ColumnName + " );");
                        break;
                    case DataType.Integer:
                        arryMtnDesign.Add("this.Controls.Add(this.txt" + dr.Table.Columns[i].ColumnName + " );");
                        break;
                    case DataType.Numeric:
                        arryMtnDesign.Add("this.Controls.Add(this.txt" + dr.Table.Columns[i].ColumnName + " );");
                        break;
                    case DataType.String:
                        arryMtnDesign.Add("this.Controls.Add(this.txt" + dr.Table.Columns[i].ColumnName + " );");
                        break;
                    default:
                        break;
                }


            }

            this.progressBar1.Value = 4;
            arryMtnDesign.Add("this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);");
            arryMtnDesign.Add("this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;");
            arryMtnDesign.Add("this.ClientSize = new System.Drawing.Size(824, 455);");
            arryMtnDesign.Add("this.Location = new System.Drawing.Point(0, 0);");
            arryMtnDesign.Add("this.Name = "+'"'+"frm"+this.txtTableName.Text    +"Mtn"+'"'+";");
            arryMtnDesign.Add("this.Text = "+'"'+"Form1"+'"'+";");
            arryMtnDesign.Add("((System.ComponentModel.ISupportInitialize)(this.errProvider)).EndInit();");
            arryMtnDesign.Add("this.ResumeLayout(false);");

            arryMtnDesign.Add("}");
            arryMtnDesign.Add("#endregion");
 
            this.progressBar1.Value = 5;
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                if (dr.Table.Columns[i].ColumnName.ToUpper() == "RECID" || dr.Table.Columns[i].ColumnName.ToUpper() == "RECSTAMP") continue;
                DataType sType = DataFunctions.GetFieldType(dbaccess, this.txtTableName.Text, dr.Table.Columns[i].ColumnName);
                arryMtnDesign.Add("private System.Windows.Forms.Label lbl" + dr.Table.Columns[i].ColumnName +";");
                switch (sType)
                {
                    case DataType.Bool:
                        arryMtnDesign.Add("private WP.Controls.TextRadioBox utr" + dr.Table.Columns[i].ColumnName + " ;");
                        break;
                    case DataType.Byte:
                        arryMtnDesign.Add("private System.Windows.Forms.TextBox txt" + dr.Table.Columns[i].ColumnName + " ;");
                        break;
                    case DataType.DateTime:
                        arryMtnDesign.Add("private WP.Controls.DateTimeEx udt" + dr.Table.Columns[i].ColumnName + ";");
                        break;
                    case DataType.Integer:
                        arryMtnDesign.Add("private System.Windows.Forms.TextBox txt" + dr.Table.Columns[i].ColumnName + " ;");
                        break;
                    case DataType.Numeric:
                        arryMtnDesign.Add("private System.Windows.Forms.TextBox txt" + dr.Table.Columns[i].ColumnName +";");
                        break;
                    case DataType.String:
                        arryMtnDesign.Add("private System.Windows.Forms.TextBox txt" + dr.Table.Columns[i].ColumnName +";");
                        break;
                    default:
                        break;
                }


            }

            arryMtnDesign.Add("}");
            arryMtnDesign.Add("}");
            this.progressBar1.Value = 6;
            dbaccess.CloseCon(); 

        }
    }
}